import {format} from '@/libs/utils'
import {
    creatProject
  } from '@/view/testPlatform/vulnerability/api/topo'
import {
    getProjects
} from '@/api/drill_compete'

export default {
    methods: {
        createTopoByTemplate(templateId, topoName = `LDYZ-${format(new Date(), 'yyyyMMddhhmmss')}`) {
            return new Promise((resolve) => {
                if (!templateId) {
                    resolve({
                        code: 0,
                        message: '请选择工程模板'
                    })
                    return
                }
                creatProject(templateId, {
                    mode: "complete",
                    name: topoName,
                    quotaTypeId: "2",
                    trainId: 0,
                    type: "normal",
                }, 'complete').then((data) => {
                    resolve(data)
                }).catch(() => {
                    resolve({
                        code: 0,
                        message: '创建拓扑图异常'
                    })
                })
            })
        },
        // 查询工程是否可用，不可用返回true
        notAvailable(id) {
            return new Promise(resolve => {
                if (!id) { // 没有数据，不允许跳转
                    resolve(true);
                }
                let params = {
                    pageIndex: 1,
                    pageSize: 10,
                    id
                }
                getProjects(params).then(data => {
                    // 存在工程，允许跳转
                    if (data.code == 1 && data.data.items.length > 0 && ![4, 17].includes(data.data.items[0].status)) {
                        resolve(false);
                    } else {
                        resolve(true);
                    }
                    resolve(true);
                }).catch(() => {
                    resolve(true);
                })
            })
        },
    }
}